//
//  ShellMineView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/5/15.
//  Copyright © 2019 吕VV. All rights reserved.
//

#import "ShellMineView.h"
#import "ShellMineCell.h"
#import "ShellMineViewModel.h"
#import "LZBaseTableView.h"
#import "ShellMineHeadView.h"
#import "ShellAboutUSViewController.h"      //关于我们
#import "ShellInvitationViewController.h"
#import "ShellRateViewController.h"         //汇率
#import "ShellChangeLoginPasswordViewController.h"   //修改登录密码
#import "ShellChangeTeadePasswordViewController.h"  //修改交易密码
#import "ShellLanguageViewController.h"         //语言
#import "ShellLoginViewController.h"            //登录页面
#import "LZBaseTabBarViewController.h"
#import "SocketRocketUtility.h"
#import "JPUSHService.h"


@interface  ShellMineView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellMineViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;

@property (nonatomic,strong) ShellMineHeadView *headView;

@end

@implementation ShellMineView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellMineViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    ShellMineHeadView *headView = [[ShellMineHeadView alloc] initViewModel:self.viewModel];
    headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 145);
  

    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    self.tableView.tableHeaderView = headView;
    UIButton *lgoutBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"退出") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
    lgoutBtn.titleLabel.font = k_textBoldFont(17);
    lgoutBtn.backgroundColor = UIColorHex(0x0D3354);
    lgoutBtn.cornerRadius = 10;
    [lgoutBtn addTarget:self action:@selector(lgoutBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_SCREENWIDTH, 90)];
    self.tableView.tableFooterView = footView;
    [footView addSubview:lgoutBtn];

    [lgoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(K_SCREENWIDTH - 30);
        make.height.mas_equalTo(50);
    }];
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self refreshUI];
        
    }];
    
    [self.viewModel getMineData];
    //刷新语言
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        [self.viewModel getMineData];
        [lgoutBtn setTitle:LZLocalizedString(@"退出") forState:UIControlStateNormal];
    }];
}



//刷新UI
- (void)refreshUI{
    [self.tableView reloadData];
}


///退出登录
- (void)lgoutBtnClick:(UIButton *)sender{
    
    [LZToolView showAlertType:LZAlertViewTypeSystemAlter withTitle:LZLocalizedString(@"是否确认退出?") message:@"" cancel:LZLocalizedString(@"取消") sure:LZLocalizedString(@"确认") objectDict:nil cancelBlock:^(id  _Nullable object) {} sureBlock:^(id  _Nullable object) {
    
        [LZToolCache saveUserInfo:[ShellUserDetailModel new]];
        LZBaseTabBarViewController *tabBarController = (LZBaseTabBarViewController *)LZTool.currentViewController.tabBarController;
        [[SocketRocketUtility instance] SRWebSocketClose];
        tabBarController.selectedIndex = 0;
        ShellLoginViewController *login = [[ShellLoginViewController alloc] init];
        LZBaseNavigationController *nav = [[LZBaseNavigationController alloc] initWithRootViewController:login];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [LZTool.currentViewController.navigationController presentViewController:nav animated:YES completion:nil];
        
        [JPUSHService setAlias:@"test0000" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {} seq:100];
    }];
    
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellMineCell *mineCell = [tableView dequeueReusableCellWithIdentifier:@"ShellMineCell"];
    mineCell.modelObject = self.viewModel.dataArray[indexPath.row];
    return mineCell;
}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
        {
            ShellInvitationViewController *invitationVC = [[ShellInvitationViewController alloc] init];
            [LZTool.currentViewController.navigationController pushViewController:invitationVC animated:YES];

        }
            break;
        case 1:
        {
            ShellRateViewController *rateVC = [[ShellRateViewController alloc] init];
            [LZTool.currentViewController.navigationController pushViewController:rateVC animated:YES];
        }
            break;
            
        case 2:
        {
            ShellChangeLoginPasswordViewController *changeLoginPasswordVC = [[ShellChangeLoginPasswordViewController alloc] init];
            [LZTool.currentViewController.navigationController pushViewController:changeLoginPasswordVC animated:YES];
        }
            break;
        case 3:
        {
            ShellChangeTeadePasswordViewController *changeTeadePasswordVC = [[ShellChangeTeadePasswordViewController alloc] init];
            [LZTool.currentViewController.navigationController pushViewController:changeTeadePasswordVC animated:YES];
        }
            break;
            
        case 4:
        {
            ShellLanguageViewController *languageVC = [[ShellLanguageViewController alloc] init];
            [LZTool.currentViewController.navigationController pushViewController:languageVC animated:YES];
        }
            break;
            
        case 6:
        {
            
            ShellAboutUSViewController *aboutUSVC = [[ShellAboutUSViewController alloc] init];
            aboutUSVC.webUrl = @"http://www.tritonlab.net/about.html";
            [LZTool.currentViewController.navigationController pushViewController:aboutUSVC animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellMineCell class] forCellReuseIdentifier:@"ShellMineCell"];
        _tableView.rowHeight = 57;
    }
    return _tableView;
}

@end
